// import window.$cookie from "js-cookie";
import { qiankunActions } from "@/qiankun/globalState";
const state = {
  sidebar: {
    opened: window.$cookie.get("sidebarStatus")
      ? !!+window.$cookie.get("sidebarStatus")
      : true,
    withoutAnimation: false,
  },
  device: "desktop",
  size: window.$cookie.get("size") || "medium",
  documentClientHeight: 0, // 页面文档可视高度(随窗口改变大小)
  lock: window.$cookie.get("lock") || "0",
};

const mutations = {
  updateDocumentClientHeight(state, height) {
    state.documentClientHeight = height;
    qiankunActions.setGlobalState({
      documentClientHeight: height,
    });
  },
  TOGGLE_SIDEBAR: (state) => {
    state.sidebar.opened = !state.sidebar.opened;
    state.sidebar.withoutAnimation = false;
    if (state.sidebar.opened) {
      window.$cookie.set("sidebarStatus", 1);
    } else {
      window.$cookie.set("sidebarStatus", 0);
    }
  },
  CLOSE_SIDEBAR: (state, withoutAnimation) => {
    window.$cookie.set("sidebarStatus", 0);
    state.sidebar.opened = false;
    state.sidebar.withoutAnimation = withoutAnimation;
  },
  TOGGLE_DEVICE: (state, device) => {
    state.device = device;
  },
  SET_SIZE: (state, size) => {
    state.size = size;
    window.$cookie.set("size", size);
  },
  SET_LOCK: (state, data) => {
    if (data === "1") {
      window.$cookie.set("lock", data);
      state.lock = data;
    } else {
      window.$cookie.remove("lock");
      state.lock = "0";
    }
  },
};

const actions = {
  toggleSideBar({ commit }) {
    commit("TOGGLE_SIDEBAR");
  },
  closeSideBar({ commit }, { withoutAnimation }) {
    commit("CLOSE_SIDEBAR", withoutAnimation);
  },
  toggleDevice({ commit }, device) {
    commit("TOGGLE_DEVICE", device);
  },
  setSize({ commit }, size) {
    commit("SET_SIZE", size);
  },
};

export default {
  namespaced: true,
  state,
  mutations,
  actions,
};
